package com.hoshiicloud.lessee.controller.category;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.auth.vo.TokenUsersVO;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.token.UserInfoUtil;
import com.hoshiicloud.goods.param.goods.GoodsPropertiesAddParam;
import com.hoshiicloud.goods.param.goods.GoodsPropertiesQueryParam;
import com.hoshiicloud.goods.param.goods.GoodsPropertiesUpdateParam;
import com.hoshiicloud.goods.vo.GoodsPropertiesPageVo;
import com.hoshiicloud.goods.vo.GoodsPropertiesVo;
import com.hoshiicloud.lessee.feign.LesseeGoodsFeignService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 2019/8/9 11:19 qry
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/category/goods-properties")
@Api(value = "品类属性-参数属性", tags = {"品类属性-参数属性"})
public class LesseeGoodsPropertiesController {
    private final LesseeGoodsFeignService goodsFeignService;

    public LesseeGoodsPropertiesController(LesseeGoodsFeignService goodsFeignService) {
        this.goodsFeignService = goodsFeignService;
    }


    @ApiOperation(value = "分页查询商品属性")
    @PostMapping("/getPage")
    public Response<Page<GoodsPropertiesPageVo>> page(@RequestBody @Valid GoodsPropertiesQueryParam param, HttpServletRequest rq) {
        param.setCompanyId(UserInfoUtil.getUserInfo(rq).getCompanyId());
        return goodsFeignService.getGoodsPropertiesPage(param);
    }


    @PostMapping("/save")
    @ApiOperation(value = "新增商品属性")
    public Response<Boolean> save(@RequestBody @Valid GoodsPropertiesAddParam param, HttpServletRequest rq) {
        TokenUsersVO user = UserInfoUtil.getUserInfo(rq);
        param.setCompanyId(user.getCompanyId());
        param.setCreatedBy(user.getRealName());
        return goodsFeignService.saveGoodsProperties(param);
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改商品属性")
    public Response update(@RequestBody @Valid GoodsPropertiesUpdateParam param, HttpServletRequest rq) {
        param.setUpdatedBy(UserInfoUtil.getUserInfo(rq).getRealName());
        return goodsFeignService.updateGoodsProperties(param);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除商品属性")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response<Boolean> delete(@PathVariable("id") Long id) {
        return goodsFeignService.deleteGoodsProperties(id);
    }

    @GetMapping("/getProperByCateId")
    public Response<List<GoodsPropertiesVo>> getProperByCateId(@RequestParam("categoryId") Long categoryId) {
        return goodsFeignService.getGoodsProperByCateId(categoryId);
    }


}
